package com.eco.fanliapp.admin.controller.web;


import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.GoodsTop;
import com.eco.fanliapp.service.IAppGoodsService;
import com.eco.fanliapp.service.IGoodsTopService;
import com.eco.fanliapp.service.ITaoBaoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/web/goodsTop")
public class GoodsTopController extends BaseController {

    @Autowired
    private IGoodsTopService goodsTopService;
    @Autowired
    private ITaoBaoService taoBaoService;
    @Autowired
    private IAppGoodsService goodsService ;

    @SysLogAnnotation(value = "增加置顶商品",userType = 2)
    @PostMapping("/addGoodsTop")
    public ServerResponse addGoodsTop(@RequestBody GoodsTop goodsTop) {
        if(getAuthentication().getRoleId() != 0){
            goodsTop.setCompanyId(getAuthentication().getCompanyId());
        }
    	return goodsTopService.addGoodsTop(goodsTop);
    }

    @GetMapping("/selectById")
    public GoodsTop selectByGoodsId(String goodsId) {

        return goodsTopService.selectByGoodsId(goodsId);

    }

    /**
     * 根据平台或关键字查询置顶商品
     * @param goodsPlatform
     * @param goodsTitle
     * @return
     */
    @GetMapping("/findGoodsTopByKeyWord")
    public ServerResponse<PageInfo> findGoodsTopByKeyWord(Integer goodsPlatform , String goodsTitle, @RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "5") Integer pageSize) {
        Integer companyId = getAuthentication().getCompanyId();
        return goodsTopService.selectGoodsTopByKeyWord(companyId,goodsPlatform,goodsTitle,currentPage,pageSize);
    }


    @GetMapping("/findAll")
    public ServerResponse<PageInfo> findAll(@RequestParam(defaultValue = "1") Integer currentPage , @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer companyId = -1;
        if(getAuthentication().getRoleId() != 0){
             companyId = getAuthentication().getCompanyId();
        }
        return goodsTopService.findAll(companyId,currentPage,pageSize);
    }

    @SysLogAnnotation(value = "删除置顶商品",userType = 2)
    @GetMapping("/deleteByGoodsId")
    public ServerResponse deleteByGoodsId(String goodsId) {
        Integer companyId = getAuthentication().getCompanyId();
    	return goodsTopService.deleteByGoodsId(goodsId);
    }
    @SysLogAnnotation(value = "删除置顶商品",userType = 2)
    @GetMapping("/deleteByGoodsIds")
    public ServerResponse deleteByGoodsId(String[] goodsIds) {
        Integer companyId = getAuthentication().getCompanyId();
    	return goodsTopService.deleteByGoodsIds(goodsIds);
    }

    /**
     * 根据商品id修改商品置顶状态
     * @param goodsId
     * @return
     */
    @SysLogAnnotation(value = "修改置顶商品",userType = 2)
    @GetMapping("/updateGoodsTopStageByGoodsId")
    public ServerResponse<PageInfo> updateGoodsTopStageByGoodsId(String goodsId , Integer goodsTopStage) {

    	return goodsTopService.updateGoodsTopStageByGoodsId(goodsId,goodsTopStage);
    }

    /**
     * 根据商品id批量修改商品置顶状态
     * @param goodsIds
     * @return
     */
    @SysLogAnnotation(value = "修改置顶商品",userType = 2)
    @GetMapping("/updateGoodsTopStageByGoodsIds")
    public ServerResponse updateGoodsTopStageByGoodsId(String[] goodsIds , Integer goodsTopStage) {

    	return goodsTopService.updateGoodsTopStageByGoodsIds(goodsIds,goodsTopStage);
    }

    /**
     * 修改置顶序号
     */
    @SysLogAnnotation(value = "修改置顶商品序号",userType = 2)
    @RequestMapping(method = RequestMethod.POST,value = "/updateSerial")
    public ServerResponse updateSerial(@RequestBody HashMap<String, Object> params){
        return goodsTopService.updateSerial(params);
    }
}
